﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Pong
{
    public class ParticleManager
    {
        private class Particle
        {
            public Color Color { get; set; }
            public Vector2 Location { get; set; }
            public int Life { get; set; }
            public Vector2 Direction{get;set;}

            public Particle(Vector2 Direction, int life, Vector2 location, Color Clr)
            {
                this.Direction = Direction;
                this.Life = life;
                this.Location = location;
                this.Color = Clr;
            }
        }
        private Texture2D Texture;
        private Random Rand = new Random();
        private LinkedList<Particle> _Particle = new LinkedList<Particle>();
        public ParticleManager(ContentManager Content)
        {
            Texture = Content.Load<Texture2D>("Puck_Glow");
        }
        public void AddParticle(int life, Vector2 Location, float BottomRadius, float TopRadius, int MaxVelocity, int amount, Color clr)
        {
            for (int i = 0; i < amount; i++)
            {
                float range = (((float)Rand.NextDouble()) * (TopRadius - BottomRadius)) + (((float)Rand.NextDouble()) * BottomRadius);
                _Particle.AddLast( new Particle(new Vector2((float)Math.Cos(range),(float) Math.Sin(range))*new Vector2( Rand.Next(0,MaxVelocity),Rand.Next(0,MaxVelocity)), life, Location, clr));
            }
            


        }
        public void Update()
        {
            List<Particle> RemovedParticles = new List<Particle>();
            foreach (Particle p in _Particle)
            {
                p.Location += p.Direction;
                p.Life -= 1;
                if (p.Life <= 0)
                {
                    RemovedParticles.Add(p); 
                }
            }
            for (int i = 0; i < RemovedParticles.Count-1; i++)
            {
                _Particle.Remove(RemovedParticles[i]);
            }
        }
        public void Draw(SpriteBatch sprite)
        {
            foreach (Particle p in _Particle)
            {
                sprite.Draw(Texture, p.Location, null, p.Color, 0.0f, new Vector2(Texture.Width / 4, Texture.Height / 4), 0.5f, SpriteEffects.None, 0);
            }
        }

    }
}
